package com.tianhui.consignortwofei.mvp.ui.activity;

import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonView;
import com.fgs.common.update.OnUpdateCallback;
import com.fgs.common.update.UpdateContext;
import com.tianhui.consignortwofei.BuildConfig;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseActivity;
import com.tianhui.consignortwofei.base.BaseFragment;
import com.tianhui.consignortwofei.constant.AppConfig;
import com.tianhui.consignortwofei.update.CheckUpdateByPGY;
import com.tianhui.consignortwofei.update.ManualCheckUpdate;
import com.xuexiang.xutil.app.AppUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2020/4/28
 * @description
 **/
public class AboutActivity extends BaseActivity {

    @BindView(R.id.activity_about_appNameTextView)
    TextView mAppNameTextView;
    @BindView(R.id.activity_about_appVersionTextView)
    TextView mAppVersionTextView;
    @BindView(R.id.activity_about_updateLinearLayout)
    LinearLayout mUpdateLinearLayout;
    UpdateContext updateContext;

    @Override
    public int getLayoutId() {
        return R.layout.activity_about;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public void init() {
        setTitle("关于");
        mAppNameTextView.setText(AppUtils.getAppName());
        mAppVersionTextView.setText("Version " + AppUtils.getAppVersionName());

        if (BuildConfig.FLAVOR.equals("officer")) {
            mUpdateLinearLayout.setVisibility(View.GONE);
        } else {
            mUpdateLinearLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public BaseFragment createFragment() {
        return null;
    }

    @OnClick(R.id.activity_about_versionUpdateSettingItemView)
    public void versionUpdate() {
        updateCheck();
    }

    private void updateCheck() {

        if (updateContext == null) {
            if (BuildConfig.FLAVOR.equals("officer")) {
                updateContext = new UpdateContext(new CheckUpdateByPGY());
            } else {
                updateContext = new UpdateContext(new ManualCheckUpdate());
            }
        }


        updateContext.checkUpdate(getContext(), new OnUpdateCallback() {
            @Override
            public void updating(String progress) {

            }

            @Override
            public void updateFailed() {

            }

            @Override
            public void updateFinish() {

            }

            @Override
            public void checkFailed() {

            }

            @Override
            public void withoutUpdate() {
                showToast("已经是最新版本！");
            }

            @Override
            public void exitUpdate() {

            }
        });
    }


    @OnClick(R.id.activity_about_downloadTextView)
    public void download() {
        String url = AppConfig.apkUrl;
        Uri uri = Uri.parse(url);
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}
